package com.imooc.jdbc.common;

import java.sql.*;

public class DbUtils {

    /**
     *
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        String drive = "com.mysql.cj.jdbc.Driver";
        Class.forName(drive);
        String url = "jdbc:mysql://192.168.7.190:3306/yulei_test?useSSL=false&characterEncoding=UTF-8&useUnicode=true";
        String dbUser = "testreadwrite";
        String dbPwd = "testrw190!@#";

        Connection conn = DriverManager.getConnection(url,dbUser, dbPwd);
        return conn;
    }


    /**
     *
     * @param rs
     * @param stmt
     * @param conn
     */
    public static void closeConnection(ResultSet rs, Statement stmt, Connection conn){
        try {
            if(rs !=null){
                rs.close();
            }

            if(stmt !=null){
                stmt.close();
            }

            if(conn != null && !conn.isClosed()){
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
